<?php
 /**
 * 功能描述：	通用
 * @datetime	2022-09-28
 * @version   	v1.0
 * @author   	ZHL
 * @copyrigh  	2022 ZHL 版权所有
 */

class Admin_CommonServe extends Model {

    /**
     * Notes:公共删除方法
     * User: ZhuHaili
     */
    public function delWhere($mouldCode, $where){
        if(!$where){
            return '请指定要删除的行';
        }
        if(!$mouldCode){
            return '请指定要删除数据';
        }
        $info = $this->conn('mysql')->findData($mouldCode, $where);
        if(empty($info)){
            return '没有找到要删除的内容';
        }
        $result = $this->conn('mysql')->delData($mouldCode, $where);
        return $result ? 'Y' : '删除失败';
    }

    /**
     * Notes:公共删除方法
     * User: ZhuHaili
     */
    public function del($mouldCode, $id){
        $id = intval($id);
        if(!$id){
            return '请指定要删除的行';
        }
        if(!$mouldCode){
            return '请指定要删除数据';
        }
        $where['id'] = $id;
        $info = $this->conn('mysql')->findData($mouldCode, $where);
        if(empty($info)){
            return '没有找到要删除的内容';
        }
        $result = $this->conn('mysql')->delData($mouldCode, $where);
        return $result ? 'Y' : '删除失败';
    }

    /**
     * Notes:批量删除方法
     * User: ZhuHaili
     */
    public function dels($mouldCode, $idStr){
        if(!$idStr){
            return '请指定要删除的行';
        }
        if(!$mouldCode){
            return '请指定要删除数据';
        }
        $where = " and id in($idStr) ";
        $result = $this->conn('mysql')->delData($mouldCode, $where);
        return $result ? 'Y' : '删除失败';
    }

    /**
     * Notes:批量修改状态
     * User: ZhuHaili
     */
    public function batchUpdateStatus($mouldCode, $idStr, $newStatus){
        if(!$idStr){
            return '请指定要操作的行';
        }
        if(!$mouldCode){
            return '请指定要操作数据';
        }
        if(!$newStatus){
            return '请指定更新后的状态';
        }
        $where = " and id in($idStr) ";
        $data['status'] = $newStatus;
        $result = $this->conn('mysql')->updateData($mouldCode, $data, $where);
        return $result ? 'Y' : '修改失败';
    }

    /**
     * Notes:批量排序
     * User: ZhuHaili
     */
    public function batchSeq($mouldCode, $param){
        if(!$param){
            return '请指定要操作的行';
        }
        if(!$mouldCode){
            return '请指定要操作数据';
        }
        foreach($param as $id=>$seq){
            $where['id'] = $id;
            $data['viewseq'] = intval($seq);
            $this->conn('mysql')->updateData($mouldCode, $data, $where);
        }
        return 'Y';
    }

    /**
     * Notes:公共排序方法
     * User: ZhuHaili
     */
    public function seq($mouldCode, $param){
        if(!is_array($param)){
            return '青选中要排序的数据';
        }
        $commM = new Admin_CommonModel();
        foreach($param as $id=>$seq){
            $where['id'] = $id;
            $data['viewseq'] = intval($seq);
            $commM->updateData($mouldCode, $data, $where);
        }
        return 'Y';
    }

    /**
     * Notes:通用-获取主副表数据列表
     * User: ZhuHaili
     */
    public function getEssayLists($mould, $where){
        if(!$mould){
            return array();
        }
        if(!$where){
            return array();
        }
        $commM = new Admin_CommonModel();
        return $commM->getEssayLists($mould, $where);
    }

    /**
     * Notes:通用分页列表-单表查询
     * User: ZhuHaili
     */
    function getListsPage($mouldCode, $pageNo, $search, $orderby=''){
        // 查询条件
        $searchInfo = SYS_SESSIONS_GET($mouldCode.'_search_info');
        $whereAdd = $searchInfo ? $searchInfo : array();
        if(!empty($search) && is_array($search)){
            $whereAdd = array();
            foreach($search as $k=>$v){
                if($k == 'search_title' && $v){
                    $whereAdd['locate']['a.title'] = $v;
                }else if($k == 'search_status' && $v){
                    $whereAdd['where']['a.status'] = $v;
                }else if($k == 'search_startdate' && $v){
                    $whereAdd['gts']['a.createdate'] = $v;
                }else if($k == 'search_enddate' && $v){
                    $whereAdd['lts']['a.createdate'] = $v;
                }
            }
            SYS_SESSIONS_SET($mouldCode.'_search_info', $whereAdd);
        }
        $commM = new Admin_CommonModel();
        // 分页条数
        $limit = 20;
        if(isset($GLOBALS['ADMIN']['sys_pages']) && intval($GLOBALS['ADMIN']['sys_pages'])){
            $limit = intval($GLOBALS['ADMIN']['sys_pages']);
        }
        // 分页计算
        $counts = $commM->getListsCount($mouldCode, $whereAdd);
        $pageSum = $counts ? ceil($counts/$limit) : 1;    // 总页数
        $pageSum = $pageSum > 1 ? $pageSum : 1;
        $pageNo = $pageNo > $pageSum ? $pageSum : $pageNo; // 最大页数==总页数
        $start = $pageNo * $limit - $limit;
        $pages['counts'] = $counts;
        $pages['pageNo'] = $pageNo;
        $pages['pageSum'] = $pageSum;
        // 获取列表数据
        $list = $commM->getListsPage($mouldCode, $whereAdd, $start, $limit, $orderby);
        $typeArr = BaseData::$logActTypeArr;
        $statusArr = BaseData::$statusArr;
        $statusArr2 = BaseData::$statusArr3;
        $statusArr3 = BaseData::$statusArr2;
        foreach($list as $k=>$v){
            if($mouldCode == 'syslog_act'){
                $list[$k]['typestxt'] = isset($typeArr[$v['types']]) ? $typeArr[$v['types']] : $v['types'];
            }
            $list[$k]['statustxt'] = isset($statusArr[$v['status']]) ? $statusArr[$v['status']] : $v['status'];
            $list[$k]['statustxt2'] = isset($statusArr2[$v['status']]) ? $statusArr2[$v['status']] : $v['status'];
            $list[$k]['statustxt3'] = isset($statusArr3[$v['status']]) ? $statusArr3[$v['status']] : $v['status'];
        }
        $result['list'] = $list;
        $result['pages'] = $pages;
        $result['where'] = $whereAdd;
        return $result;
    }

    /**
     * Notes:通用列表导出-单表查询
     * User: ZhuHaili
     */
    function getListExecl($mouldCode, $pageNo, $idStr, $search, $thList, $orderby){
        // 查询条件
        $searchInfo = SYS_SESSIONS_GET($mouldCode.'_search_info');
        $whereAdd = $searchInfo ? $searchInfo : array();
        if(!empty($search) && is_array($search)){
            $whereAdd = array();
            foreach($search as $k=>$v){
                if($k == 'search_title' && $v){
                    $whereAdd['locate']['a.title'] = $v;
                }else if($k == 'search_status' && $v){
                    $whereAdd['a.status'] = $v;
                }else if($k == 'search_startdate' && $v){
                    $whereAdd['gts']['a.createdate'] = $v;
                }else if($k == 'search_enddate' && $v){
                    $whereAdd['lts']['a.createdate'] = $v;
                }
            }
            SYS_SESSIONS_SET($mouldCode.'_search_info', $whereAdd);
        }
        if($idStr){
            $whereAdd['in']['a.id'] = $idStr;
        }
        $commM = new Admin_CommonModel();
        $start = 0;
        $limit = 0;
        if($pageNo){
            // 分页条数
            $limit = 20;
            if(isset($GLOBALS['ADMIN']['sys_pages']) && intval($GLOBALS['ADMIN']['sys_pages'])){
                $limit = intval($GLOBALS['ADMIN']['sys_pages']);
            }
            // 计算分页
            $counts = $commM->getListsCount($mouldCode, $whereAdd);
            $pageSum = $counts ? ceil($counts/$limit) : 1;    // 总页数
            $pageSum = $pageSum > 1 ? $pageSum : 1;
            $pageNo = $pageNo > $pageSum ? $pageSum : $pageNo; // 最大页数==总页数
            $start = $pageNo * $limit - $limit;
        }
        $thList['createdate'] = '创建时间';
        $trList = $commM->getListExecl($mouldCode, $whereAdd, $start, $limit, $orderby);
        $typeArr = BaseData::$logActTypeArr;
        $statusArr = BaseData::$statusArr;
        $statusArr2 = BaseData::$statusArr3;
        $statusArr3 = BaseData::$statusArr2;
        foreach($trList as $k=>$v){
            $trList[$k] = $v;
            if($mouldCode == 'syslog_act') {
                $trList[$k]['typestxt'] = isset($typeArr[$v['types']]) ? $typeArr[$v['types']] : $v['types'];
            }
            $trList[$k]['statustxt'] = isset($statusArr[$v['status']]) ? $statusArr[$v['status']] : $v['status'];
            $trList[$k]['statustxt2'] = isset($statusArr2[$v['status2']]) ? $statusArr2[$v['status2']] : $v['status2'];
            $trList[$k]['statustxt3'] = isset($statusArr3[$v['status3']]) ? $statusArr3[$v['status3']] : $v['status3'];
        }
        return array('thList'=>$thList,'trList'=>$trList, 'page'=>$pageNo);
    }

    /**
     * Notes:新增文件上传日志
     * User: ZhuHaili
     */
    function addFileLog($paths){
        if(!$paths){
            return false;
        }
        $createdate = date('Y-m-d  H:i:s');
        $data['createdate'] = $createdate;
        $data['title'] = $paths;
        $commonM = new Admin_CommonModel();
        return $commonM->addFileLog($data);
    }

}